//
//  SpeedMessage.swift
//  Trans
//
//  Created by zcj on 2024/11/12.
//  设置传输速度的信息的，实际如何由发送方来判断如何执行，不用回信

import Foundation
import Combine

class SpeedMessage:BaseData{
    var speed:FSIZE
    
    override init(){
        speed = 0
        super.init()
    }
    init(speed:FSIZE){
        self.speed = speed
        super.init()
    }
    
    enum CodingKeys: String, CodingKey{
        case speed
    }
    
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy:CodingKeys.self)
        speed = try container.decode(FSIZE.self, forKey: .speed)
        try super.init(from: decoder)
    }
    
    override func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(speed, forKey: .speed)
        try super.encode(to: encoder)
    }
    
    static func packageData(speedMsg:SpeedMessage)  -> Data?{
        let jsonData = try? SpeedMessage.enCodeToJson(obj: speedMsg)
        //生成包头
        let ph = PackageHead()
        ph.type = UINT32(SPEED_REQ)
        ph.dataCount = PackageHead.headCount+UINT32(jsonData!.count)
        ph.msgnum = 1
        var phData = ph.encode()
        
        phData.append(jsonData!)
        return phData
    }
    
    override func getType() -> UINT32 {
        return SPEED_REQ;
    }
}
